package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.domain.*;
import com.it.service.AccountService;
import com.it.service.AdminUsersService;
import com.it.service.BillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("adminUser")
public class AdminUsersController {
    @Autowired
    private AdminUsersService adminUsersService;
    @Autowired
    private BillsService billsService;

    /**
     * 跳转页面
     * @param mv
     * @return
     */
    @RequestMapping("/tiaozhuanUserLogin")
    public ModelAndView tiaozhuanUserLogin(ModelAndView mv){
        mv.setViewName("UserLogin");
        return mv;
    }

    /**
     * 登录
     * @param mv
     * @param request
     * @param response
     * @param adminUsers
     * @return
     */
    @RequestMapping("/login.do")
    public ModelAndView UserLogin(ModelAndView mv, HttpServletRequest request,
                                  HttpServletResponse response, AdminUsers adminUsers) {
        adminUsers.setUsername(request.getParameter("username"));
        adminUsers.setPassword(request.getParameter("password"));

        try{
            List<AdminUsers> adminUsersList = adminUsersService.UserLogin(adminUsers);
            String position = adminUsersList.get(0).getPosition();
            if (position.equals("管理岗")){
                mv.setViewName("Admin_login");

            }else {
                mv.setViewName("UserGoods");
            }
        }catch (Exception e){
            String st = "登录失败！<a href=\"tiaozhuanUserLogin\">重新登录</a>";
            mv.addObject("return_as", st);
            mv.setViewName("returnBackNull");
        }



        return mv;
    }

    /**
     * 注册
     * @param mv
     * @param request
     * @param response
     * @param adminUsers
     * @return
     */
    @RequestMapping("/regist.do")
    public ModelAndView UserRegist(ModelAndView mv, HttpServletRequest request,
                                   HttpServletResponse response, AdminUsers adminUsers) throws ParseException {
        adminUsers.setName(request.getParameter("name"));
        adminUsers.setUsername(request.getParameter("username"));
        adminUsers.setPassword(request.getParameter("password"));
        adminUsers.setGender(request.getParameter("gender"));
        adminUsers.setPhoneNum(request.getParameter("phoneNum"));
        //日期转换
        DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
        Date date = df.parse(request.getParameter("Entry_date"));
        adminUsers.setEntry_date(date);
        adminUsers.setWage(Double.parseDouble(request.getParameter("wage")));
        adminUsers.setPosition(request.getParameter("position"));

        adminUsersService.UserRegist(adminUsers);

        String st = "注册成功！";
        mv.addObject("return_as", st);
        mv.setViewName("returnBackUser");
        return mv;

    }

    /**
     * 查询所有users
     * @param page
     * @param size
     * @param mv
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    @RequestMapping("/findAll.do")
    public ModelAndView FindAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                      @RequestParam(name = "size", required = true, defaultValue = "5") int size,
                                      ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        List<AdminUsers> adminUsers = adminUsersService.FindAll(page, size);
        PageInfo pageInfo = new PageInfo(adminUsers);
        mv.addObject("AllUserList",pageInfo);
        mv.setViewName("User_FindAll");
        return mv;
    }

    /**
     * 根据id查询用户
     * @param mv
     * @param request
     * @param response
     * @param adminUsers
     * @return
     * @throws ParseException
     */
    @RequestMapping("/findById.do")
    public ModelAndView FindById(ModelAndView mv, HttpServletRequest request,
                                   HttpServletResponse response, AdminUsers adminUsers) throws ParseException {
        List<AdminUsers> id = adminUsersService.FindById(Integer.parseInt(request.getParameter("id")));
        AdminUsers adminUsers1 = id.get(0);
        mv.addObject("findByIdList",adminUsers1);
        mv.setViewName("User_change");
        return mv;

    }

    /**
     * 根据id修改user信息
     * @param mv
     * @param request
     * @param response
     * @param adminUsers
     * @return
     */
    @RequestMapping("/changeById.do")
    public ModelAndView ChangeById(ModelAndView mv, HttpServletRequest request,
                                 HttpServletResponse response, AdminUsers adminUsers){
        try {
        adminUsers.setId(Integer.parseInt( request.getParameter("id")));
        adminUsers.setName(request.getParameter("name"));
        adminUsers.setUsername(request.getParameter("username"));
        adminUsers.setPassword(request.getParameter("password"));
        adminUsers.setGender(request.getParameter("gender"));
        adminUsers.setPhoneNum(request.getParameter("phoneNum"));
        String entry_date = request.getParameter("Entry_date");
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        df.setLenient(false);

        adminUsers.setEntry_date(df.parse(entry_date));
        adminUsers.setWage(Double.parseDouble(request.getParameter("wage")));
        adminUsers.setPosition(request.getParameter("position"));


        adminUsersService.changeById(adminUsers);
            String st = "操作成功！";
            mv.addObject("return_as", st);
            mv.setViewName("returnBackUser");
            return mv;

        } catch (ParseException e) {
            String st = "操作失败！";
            mv.addObject("return_as", st);
            mv.setViewName("returnBackUser");
            return mv;
        }

    }

    /**
     * 根据id删除user
     * @param mv
     * @param request
     * @param response
     * @param adminUsers
     * @return
     */
    @RequestMapping("/deleById.do")
    public ModelAndView DelById(ModelAndView mv, HttpServletRequest request,
                                   HttpServletResponse response, AdminUsers adminUsers){
        adminUsersService.deleById(Integer.parseInt(request.getParameter("id")));
        String st = "操作成功！";
        mv.addObject("return_as", st);
        mv.setViewName("returnBackUser");
        return mv;
    }

    /**
     * 根据姓名查询
     * @param page
     * @param size
     * @param mv
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    @RequestMapping("/findByName.do")
    public ModelAndView FindByName(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "5") int size,
                                ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        try {
            List<AdminUsers> name = adminUsersService.findByName(request.getParameter("name"), page, size);
            PageInfo pageInfo = new PageInfo(name);
            mv.addObject("findByNameList", pageInfo);
            mv.setViewName("User_FindByName");
            return mv;
        }catch (Exception e){
            String st = "操作失败！";
            mv.addObject("return_as", st);
            mv.setViewName("returnBackUser");
            return mv;
        }

    }

    /**
     * 发工资
     * @param mv
     * @param request
     * @param response
     * @param adminUsers
     * @return
     */
    @RequestMapping("/salaryOut.do")
    public ModelAndView SalaryOut(ModelAndView mv, HttpServletRequest request, Bills bills, Employee_salary employee_salary,
                                HttpServletResponse response, AdminUsers adminUsers){
        int id = Integer.parseInt(request.getParameter("id"));
        List<AdminUsers> adminUsers1 = adminUsersService.FindById(id);
        AdminUsers adminUsers2 = adminUsers1.get(0);
        double wage = adminUsers2.getWage();
        //记录Bills
        bills.setPay_out(wage);
        bills.setCredit(0);
        bills.setProfit(0-wage);
        Date date = new Date();
        bills.setOperation_date(date);
        bills.setReason("给"+adminUsers2.getName()+"发工资："+wage+"元");
        billsService.insertBills(bills);

        //记录员工工资发售表
        employee_salary.setName(adminUsers2.getName());
        employee_salary.setPosition(adminUsers2.getPosition());
        employee_salary.setSalary_date(date);
        employee_salary.setWage(adminUsers2.getWage());
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        employee_salary.setReason("发工资："+wage+"元，日期"+df.format(date));

        adminUsersService.insertSalary(employee_salary);

        String st ="增加成功";
        mv.addObject("RetrunSt",st);
        mv.setViewName("User_FindAll");
        return mv;

    }

    /**
     * 查询所有salary
     * @param page
     * @param size
     * @param employee_salary
     * @param mv
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    @RequestMapping("/findAllSalary.do")
    public ModelAndView FindAllSalary(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                   @RequestParam(name = "size", required = true, defaultValue = "5") int size,Employee_salary employee_salary,
                                   ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        List<Employee_salary> employee_salaries = adminUsersService.FindAllSalary(page, size);
        PageInfo pageInfo = new PageInfo(employee_salaries);
        mv.addObject("Retrunlist",pageInfo);
        mv.setViewName("User_FindAllSalary");
        return mv;

    }

    /**
     * 根据姓名模糊查询salary
     * @param page
     * @param size
     * @param employee_salary
     * @param mv
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    @RequestMapping("/findAllSalaryByName.do")
    public ModelAndView FindAllSalaryByName(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                      @RequestParam(name = "size", required = true, defaultValue = "5") int size,Employee_salary employee_salary,
                                      ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        List<Employee_salary> name = adminUsersService.FindAllSalaryByName(request.getParameter("name"), page, size);
        PageInfo pageInfo = new PageInfo(name);
        mv.addObject("RetrunlistByName",pageInfo);
        mv.setViewName("User_FindAllSalaryByName");
        return mv;


    }

}
